import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import fs from 'fs-extra'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
//预编译element-plus大部分组件
let elementComponents: string[] = []
try {
  fs.readdirSync(resolve('./', 'node_modules/element-plus/es/components')).forEach(
    (dirName) => {
      try {
        fs.accessSync(
          `node_modules/element-plus/es/components/${dirName}/style/index.mjs`,
          fs.constants.F_OK
        )
        elementComponents.push(`element-plus/es/components/${dirName}/style/index`)
      } catch (err) {
        console.log('File does not exist.',dirName)
      }
    }
  )
} catch (error) { }
try {
  fs.readdirSync(resolve('./', 'node_modules/element-plus/es/components')).forEach(
    (dirName) => {
      try {
        fs.accessSync(
          `node_modules/element-plus/es/components/${dirName}/style/css.mjs`,
          fs.constants.F_OK
        )
        elementComponents.push(`element-plus/es/components/${dirName}/style/css`)
      } catch (err) {
        console.log('File does not exist.',dirName)
      }
    }
  )
} catch (error) { }
console.log(
  '%c                 _oo0oo_                     \n' +
  '                  o8888888o										\n' +
  '                  88" . "88										\n' +
  '                  (| -_- |)										\n' +
  '                   O\\ = /O										\n' +
  "               ____/`---'\\____									\n" +
  "             .   ' \\\\| |// `.									\n" +
  '              / \\\\||| : |||// \\								\n' +
  '           / _||||| -胡神- |||||- \\								\n' +
  '              | | \\\\\\ - /// | |								\n' +
  "            | \\_| ''\\---/'' | |								\n" +
  '             \\ .-\\__ `-` ___/-. /							\n' +
  "          ___`. .' /--.--\\ `. . __							\n" +
  '       ."" "< `.___\\_<|>_/___. ` >" "".        \n' +
  '      | | : `- \\`.;`\\ _ /`;.`/ - ` : | |       \n' +
  '        \\ \\ `-. \\_ __\\ /__ _/ .-` / /          \n' +
  "======`-.____`-.___\\_____/___.-`____.-'======  \n" +
  "                   `=---='                     \n" +
  '.............................................  \n\t\t' +
  '佛祖镇楼                  BUG辟易                          \n\t' +
  '佛曰:\n\t\t' +
  '写字楼里写字间，写字间里程序员；\n\t\t' +
  '程序人员写程序，又拿程序换酒钱。\n\t\t' +
  '酒醒只在网上坐，酒醉还来网下眠；\n\t\t' +
  '酒醉酒醒日复日，网上网下年复年。\n\t\t' +
  '但愿老死电脑间，不愿鞠躬老板前；\n\t\t' +
  '奔驰宝马贵者趣，公交自行程序员。\n\t\t' +
  '别人笑我忒疯癫，我笑自己命太贱；\n\t\t' +
  '不见满街漂亮妹，哪个归得程序员？')
export default defineConfig({
  plugins: [
    vue(),
    createSvgIconsPlugin({
      iconDirs: [resolve(process.cwd(), "src/assets/svg")], //路径
      symbolId: "icon-[dir]-[name]"
    }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    })
  ],
  css: {
    // css预处理器
    preprocessorOptions: {
      scss: {
        // 引入 mixin.scss 这样就可以在全局中使用 mixin.scss中预定义的变量了
        // 给导入的路径最后加上 ; 
        additionalData: `
        @import "@/assets/style/mixin.scss";
        `
      }
    }
  },
  optimizeDeps: {
    include: [
      "pinia",
      "sass",
      "vue-router",
      'element-plus',
      "uuid",
      "lodash",
      "dayjs",
      "mockjs",
      "element-plus/es",
      ...elementComponents
    ],
    exclude: []
  },
  resolve: {
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
    alias: {
      "@": resolve(__dirname, "./src")
    }
  }
})
